
import logging
from library.Utils import Utils


class Log(logging.getLoggerClass()):
    '''
    Override to logger class
    '''
    __logFileName__ = "macysdbtesting.log"
    __obsoluteLogFilePath__ = ""
    __logger__ = ""
    __loggingType__ = logging.DEBUG
    __fileloggingType__ = logging.DEBUG
    __consoleloggingType__ = logging.DEBUG

    def __init__(self):
        self.__getLogFilePath__();
        #self.getLogger()
       
    def __getLogFilePath__(self):
        self.__obsuluteLogFilePath__ = Utils().baseAppPath() + self.__logFileName__
         
    def getLogger(self,loggerName="DefaultLogger"):
        self.__logger__ = logging.getLogger(loggerName)
        
        #self.basicLogger()
        self.fileLogger(loggerName)
#        self.consoleLogger()
        
        
        return self.fileLogger(loggerName)
        #return self.__logger__
    
    def consoleLogger(self, loggerName="ConsoleLogger"):
        try:
            console = logging.StreamHandler()
            #console.setLevel(self.__consoleloggingType__)
            formatter = logging.Formatter('%(name)-12s:%(levelname)s %(message)s')
            console.setFormatter(formatter)
            logging.getLogger(loggerName).addHandler(console)
            self.__logger__.setLevel(self.__consoleloggingType__)
        except Exception as e:
            print "Unable to create Console stream Handler " + str(e)
    
    def fileLogger(self,loggerName="FileLogger"):
        try:
            self.__logger__ = logging.getLogger(loggerName)
            hdlr = logging.FileHandler(self.__obsuluteLogFilePath__)
            formatter = logging.Formatter("%(asctime)s::%(levelname)s - File::%(name)s - Line::%(lineno)d - Function::%(funcName)s - %(message)s")
            hdlr.setFormatter(formatter)
            hdlr.setLevel(self.__fileloggingType__)
            self.__logger__.addHandler(hdlr)
            return self.__logger__
        except Exception as e:
            print "Unable to create file logger instance" + str(e)
            
    def basicLogger(self,loggerName = "BasicLogger"):
        logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s::%(levelname)s - File::%(name)s - Line::%(lineno)d - Function::%(funcName)s - %(message)s',
                    datefmt='%m-%d %H:%M',
                    filename=self.__obsoluteLogFilePath__,
                    filemode='w')